package com.ricchieri.manage.bean;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.ReservationFilter;
import com.ricchieri.model.Reservation;
import com.ricchieri.reports.ReportFactory;
import com.ricchieri.reports.ReportFileFormat;
import com.ricchieri.reports.ReportTemplate;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "viewReservationMB")
@ViewScoped
public class ViewReservationManageBean extends GenericManageBean {

	private static final long serialVersionUID = 5531296565698443473L;
	
	private Reservation reservation;
	private ReservationFilter reservationFilter;
	
	@PostConstruct
	public void init() {
		reservation = (Reservation) FacesUtil.getParametersFlash(BusinessSettings.KEY_RESERVATION);
		reservationFilter = (ReservationFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		
		if(reservation == null) {
			FacesUtil.redirectToNavigationCase(BusinessSettings.SEARCH_RESERVATION);
		}
	}

	public Reservation getReservation() {
		return reservation;
	}

	public void setReservation(Reservation reservation) {
		this.reservation = reservation;
	}
	
	public void downloadReport() {
		ReportTemplate report = ReportFactory.getReport(BusinessSettings.RESERVATION_REPORT);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(BusinessSettings.KEY_RESERVATION, getReservation());
		report.generateReport(map , ReportFileFormat.PDF);
	}
	
	public String createSameSchedule() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_RESERVATION, reservation);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_ADD_RESERVATION, BusinessSettings.KEY_ADD_RESERVATION_SCHEDULE);
		return BusinessSettings.ADD_RESERVATION;
	}
	
	public String createSameUser() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_RESERVATION, reservation);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_ADD_RESERVATION, BusinessSettings.KEY_ADD_RESERVATION_USER);
		return BusinessSettings.ADD_RESERVATION;
	}
	
	public String doBackToSearchReservation() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, reservationFilter);
		return BusinessSettings.SEARCH_RESERVATION;
	}
}
